package com.daiqee.service.impl;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import com.daiqee.pojo.User;
import com.daiqee.pojo.UserDepartment;
import com.daiqee.pojo.UserRole;
import com.baomidou.mybatisplus.service.IService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.daiqee.dao.mapper.UserDepartmentMapper;
import com.daiqee.dao.mapper.UserMapper;
import com.daiqee.dao.mapper.UserRoleMapper;

/**
 * 用户
 * 
 * <br/>
 * @since JDK 1.8
 * @version  V2.1
 * @author RichardTang 
 * @date: 2018年5月8日 上午8:29:15
 * @package com.daiqee.service.impl
 * @copyright:Copyright (c) 2018, 1245811923@qq.com All Rights Reserved.
 */
@Service
public class UserService extends ServiceImpl<UserMapper, User> implements IService<User>{
	
	@Autowired
	private UserMapper userMapper;
	
	@Autowired
	private UserDepartmentMapper userDepartmentMapper;
	
	@Autowired
	private UserRoleMapper userRoleMapper;
	
	/**
	 * 添加一个管理员账号
	 * @Time 2018-1-20
	 * @author RichardTang
	 * @param admin
	 * 				给定一个管理员账号实体,来添加！实体中存在参数
	 * @return 返回受影响的数据数量,0:修改失败,1:修改成功
	 */
	public Integer addUser(User user) {
		Integer result = 0;
		result+= userMapper.insert(user);
		result+= userRoleMapper.insert(new UserRole(user.getUserId(),user.getUserRole()));
		result+= userDepartmentMapper.insert(new UserDepartment(user.getUserId(), user.getUserDepartment()));
		return result;
	}
	
	/**
	 * 修改后台登陆账号的密码
	 * @Time 2018-1-20
	 * @author RichardTang
	 * @param username
	 * 					需要被修改的账户用户名
	 * @param oldpassword
	 * 					需要被修改账户的密码
	 * @param newpassword
	 * 					需要修改的新密码
	 * @return 返回受影响的数据数量,0:修改失败,1:修改成功
	 */
	public Integer updatePwdByAdmin(String username,String oldpassword,String newpassword) {
		return userMapper.updatePwdByAdmin(username, oldpassword, newpassword);
	}
	
	/**
	 * 更新账户信息操作
	 * @since 2.1
	 * @version 2.1
	 * @Time 2018年4月3日08:52:50
	 * @author RichardTang
	 * @param user 
	 * 			用户实体
	 * @return 返回受影响的数据数量,0:修改失败,1:修改成功
	 */
	public Integer updateUser(User user) {
		//更新用户
		userMapper.updateById(user);
		//更新关系(角色)
		userRoleMapper.updateById(new UserRole(user.getUserId(), user.getUserRole()));
		//更新关系(部门)
		userDepartmentMapper.updateById(new UserDepartment(user.getUserId(),user.getUserDepartment()));
		return 1;
	}
}

